package com.xml;

import com.operation.SQLOperationMap;


public class TextSQLRecordExtract  extends Extract{


	public static final String SPLIT_FLAG = "&JSON&";


	// 循环访问所有节点，输出包含关键字的值节点
	// &JSON&
	/**
	 * TEXT:insert&JSON&A=B&JSON&C=D
	 */
	public SQLOperationMap extractOperationMap(String text, long storeFrom)
			throws Exception {
		// 取得Map的条数
		SQLOperationMap opMap = new SQLOperationMap(storeFrom);
		SQL sql = getRecordListByText(text, storeFrom);
		opMap.setSql(sql.sql);
		opMap.param = sql.list;
		return opMap;
	}

	

	private SQL getRecordListByText(String txt, long storeFrom) {
		SQL aSQL = new SQL();
		int index = txt.indexOf(SPLIT_FLAG);
		String sql = txt.substring(5, index);
		String sub = txt.substring(index + SPLIT_FLAG.length());
		String[] params = sub.split(SPLIT_FLAG);
		int rowCount = params.length;
		aSQL.sql = sql;

		// // 以后都是数据
		if (rowCount > 1) {
			for (int i = 1; i < rowCount; i++) {
				int eqIndex = params[i].indexOf("=");
				String first = params[i].substring(0, eqIndex);
				String last = params[i].substring(eqIndex + 1);
				SQLparam aSQLParam = new SQLparam();
				aSQLParam.setType(first);
				aSQLParam.setObj(last);
				aSQL.list.add(aSQLParam);
			}
		}
		return aSQL;
	}

}
